package com.example.kakaotest;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.kakaotest.manager.PropertyManager;
import com.example.sampleasmacktest.User;
import com.example.sampleasmacktest.XMPPManager;
import com.example.sampleasmacktest.XMPPManager.OnLoginListener;
import com.example.sampleasmacktest.XMPPManager.OnRosterListener;


public class TabFourFragment extends Fragment {
	static final String TAG="kakaoTest";
	TextView id, pw;
	PropertyManager man;
	ArrayAdapter<User> mAdapter;
	List<User> mUsers;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public void onPause() {
		man = PropertyManager.getInstance();
		String strId = man.getUserName();
		Log.i(TAG, "ID: "+strId);
		id.setText(strId);
		pw.setText(man.getUserPw());
		super.onPause();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		TabOneFragment fragment = 
				(TabOneFragment) getFragmentManager().findFragmentByTag("spec1");
		mUsers = fragment.users;
		mAdapter = fragment.mAdapter;
		View v = inflater.inflate(R.layout.tab_fragment_layout, container, false);
		id = (TextView)v.findViewById(R.id.idEdit);
		pw = (TextView)v.findViewById(R.id.pwEdit);
		man = PropertyManager.getInstance();
		String strId = man.getUserName();
		Log.i(TAG, "ID: "+strId);
//		id.setText(strId);
//		pw.setText(man.getUserPw());
		final Button btn = (Button)v.findViewById(R.id.login);
		if (strId == null || "".equals(strId)){
			btn.setText("로그인");
			id.setEnabled(true);
			pw.setEnabled(true);
		} else { 
			btn.setText("로그아웃");
			id.setEnabled(false);
			pw.setEnabled(false);
		}
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (btn.getText().equals("로그인")){
					String strId = id.getText().toString();
					String strPw = pw.getText().toString();
					loginProcess(strId, strPw);
//					man.setUserName(strId);
//					man.setUserPw(strPw);
				}else {
					man.setUserName("");
					man.setUserPw("");
				}
				MainActivity activity = (MainActivity) getActivity();
				activity.tabs.setCurrentTabByTag("spec1");
			}
		});

		return v;
	}
	
	public boolean loginProcess(final String id, final String pw) {
		XMPPManager.getInstance().login(id, pw, new OnLoginListener() {
			
			@Override
			public void onLoginSuccess(String username) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity(), "login success", Toast.LENGTH_SHORT).show();
				XMPPManager.getInstance().getRoster(new OnRosterListener() {
					
					@Override
					public void onRoasterReceived(List<User> users) {

						for (User user:users){
							Log.i(TAG, "name: "+user.getUser().getName());
							mUsers.add(user);
						}
						mAdapter.notifyDataSetChanged();
					}
				});
				man.setUserName(id);
				man.setUserPw(pw);
			}
			
			@Override
			public void onLoginFail(String username) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity(), "login failed", Toast.LENGTH_SHORT).show();
			}
		});
		
		return true;
	}
	
}
